Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: make callback opaque #1383

Closed
wants to merge 6 commits into from

Conversation

wyfo
Copy link
Contributor

@wyfo wyfo commented Sep 9, 2024

Use an enum hidden to use channel senders directly. It should improve performances, and may allow later to use async sender methods.

Use an enum hidden to use channel senders directly.
It should improve performances, and may allow later to use async sender methods.
@wyfo wyfo requested a review from OlivierHecart September 9, 2024 15:30
Copy link

github-actions bot commented Sep 9, 2024

PR missing one of the required labels: {'dependencies', 'documentation', 'breaking-change', 'bug', 'enhancement', 'internal', 'new feature'}

@wyfo wyfo requested a review from Mallets September 9, 2024 15:30
@wyfo wyfo added enhancement Existing things could work better breaking-change Indicates that the issue implies a breaking change (be it at compile time or at runtime) labels Sep 9, 2024
{
match &self.0 {
CallbackInner::Dyn(cb) => cb(arg),
CallbackInner::Flume(tx) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure that this kind of optimization is really justified given that flume/ring channels are just one of many that users can use in rust, and that many people are still going to just use function callbacks ?
Also to make use of such an optimization we would need to complicate zenoh-c interface since right now it only accepts a "closure" with pointer to c-function (which is transformed into Arc).

Copy link
Contributor Author

@wyfo wyfo Sep 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flume is not "just one of many that users can use in rust", it's the default one, and one of the few with a IntoHandler implementation (the others being RingChannel, which is also included in CallbackInner, std:sync::mpsc).
And the goal of this change is also to optimize callback use, see https://godbolt.org/z/G6vcT4b93.
But more than a performance optimization, the long-term vision is to be able to get flume sender directly in order to use its async method.

Also to make use of such an optimization we would need to complicate zenoh-c interface since right now it only accepts a "closure" with pointer to c-function (which is transformed into Arc).

I don't see the how this PR changes anything here

Copy link
Contributor

@DenisBiryukov91 DenisBiryukov91 Sep 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see the how this PR changes anything here

It adds an extra overhead due to enum match (not sure whether it is significant though). Which means that you probably improve performance of flume channel in rust, but maybe also make it worse for the whole zenoh-c/cpp + produce slightly more complex code.
This also means that if in several month we decide to have another "default" channel we would need to revisit this code.

What I mean is that I would really like to see the performance gain for channels / loss for callbacks if there is any.

Copy link
Contributor Author

@wyfo wyfo Sep 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It adds an extra overhead due to enum match (not sure whether it is significant though).

Branch prediction enters the room.

Which means that you probably improve performance of flume channel in rust, but maybe also make it worse for the whole zenoh-c/cpp + produce slightly more complex code.

Still better than the previous "by value" callback, see godbolt. Why is the produced code more complex?

This also means that if in several month we decide to have another "default" channel we would need to revisit this code.

And then, we will change the internal enum, we don't care, it's internal, and it's only a dozen lines to change.

What I mean is that I would really like to see the performance gain for channels / loss for callbacks if there is any.

Trust branch prediction, especially if you are using only callbacks.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

till better than the previous "by value" callback, see godbolt. Why is the produced code more complex?

I do not argue that passing by ref is faster than by value, this can still be achieved through only Arc, without any enum matching.
The produced code is more complex since it adds more lines => higher maintenance cost (which is always better to be avoided if there is no any significant benefit)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But more than a performance optimization, the long-term vision is to be able to get flume sender directly in order to use its async method.

This is the goal behind making the type opaque. Now, using the enum is not a high maintenance code, all the code is contained in one module and is only a few lines.

@wyfo wyfo marked this pull request as draft September 11, 2024 07:55
@wyfo
Copy link
Contributor Author

wyfo commented Sep 11, 2024

Using a immutable reference as callback argument prevent getting a mutable access to a shared memory segment contained in payload.
The solutions would be either to use a mutable reference, or to keep the argument by value. @kydos said he prefer keeping the current semantic, so let's go. I will the PR accordingly.

@DenisBiryukov91
Copy link
Contributor

Using a immutable reference as callback argument prevent getting a mutable access to a shared memory segment contained in payload.

Access by value is still available through handlers if needed.

@Mallets
Copy link
Member

Mallets commented Sep 11, 2024

Using a immutable reference as callback argument prevent getting a mutable access to a shared memory segment contained in payload. The solutions would be either to use a mutable reference, or to keep the argument by value. @kydos said he prefer keeping the current semantic, so let's go. I will the PR accordingly.

With current semantics you mean passing Sample by value?

@wyfo
Copy link
Contributor Author

wyfo commented Sep 11, 2024

Yes

@wyfo
Copy link
Contributor Author

wyfo commented Sep 12, 2024

Replaced by #1405

@wyfo wyfo closed this Sep 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking-change Indicates that the issue implies a breaking change (be it at compile time or at runtime) enhancement Existing things could work better
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants